package com._1.message.redis;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class MyRedisLockService {

    @Autowired
    private CustomRedisLock customRedisLock;

    public void doSomething() {

        String lockKey = "lockKey";
        String requestId = UUID.randomUUID().toString();
        long expireTime = 30000;

        try{

            if(customRedisLock.tryLock(lockKey, requestId, expireTime)){
                System.out.println("获取锁成功 开始执行业务逻辑");
            }else{
                System.out.println("获取锁失败");
            }

        }finally {
            //释放锁
            if(customRedisLock.releaseLockAtomic(lockKey, requestId)){
                System.out.println("释放锁成功");
            }else{
                System.out.println("释放锁失败");
            }
        }
    }

}
